﻿//-----------------------------------------------------------------------
// <copyright file="XmlActionResult.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.ContactManager.WebSite.Infrastructure.ActionResults
{
    using System.Text;
    using System.Web.Mvc;
    using System.Xml;
    using System.Xml.Serialization;

    public class XmlActionResult : ContentResult
    {
        public XmlActionResult(object model)
        {
            base.ContentType = "application/xml";
            base.Content = this.SerializeObjectToXml(model);
        }

        private string SerializeObjectToXml(object model)
        {
            var sb = new StringBuilder();
            var serializer = new XmlSerializer(model.GetType());
            using(var writer = XmlWriter.Create(sb))
                serializer.Serialize(writer, model);
            return sb.ToString();
        }

    }
}